/**
 * Filename: AlbumMemberAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/8 12:11
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.InviteChooseActivity;
import com.github.postsanf.yinian.activity.UserStatusActivity;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.bean.YNMember;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


public class ChatMemberAdapter extends RecyclerView.Adapter<ChatMemberAdapter.ViewHolder> {

    private Context mContext;
    private List<YNMember> datas;
    private YNAlbum mGroup;

    public ChatMemberAdapter(Context mContext, List<YNMember> datas, YNAlbum mGroup) {
        this.mContext = mContext;
        this.mGroup = mGroup;
        this.datas = datas;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_item_album_member;                                //列表View
        SimpleDraweeView iv_item_album_member_avatar;                        //成员头像
        TextView tv_item_album_member_username;                        //成员名

        public ViewHolder(View itemView) {
            super(itemView);
            ll_item_album_member = (LinearLayout) itemView
                    .findViewById(R.id.ll_item_album_member);
            iv_item_album_member_avatar = (SimpleDraweeView) itemView.findViewById(R.id.iv_item_album_member_avatar);
            tv_item_album_member_username = (TextView) itemView
                    .findViewById(R.id.tv_item_album_member_username);
        }

        public void bindData(final YNMember listItems, int position) {
            if (listItems != null) {
                if (listItems.getUser().getUnickname().equals("Add成员") ) {
                    tv_item_album_member_username.setText("");
                    iv_item_album_member_avatar.setImageResource(R.drawable.chat_member_add);
                    iv_item_album_member_avatar.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            YNAlbum listItems = YNSingleton.getInstance().getCurAlbum();
                            Intent intent = new Intent(mContext, InviteChooseActivity.class);
                            intent.putExtra(CommonConstants.YNDATAS, listItems);
                            mContext.startActivity(intent);
                        }
                    });
                } else {
                    ImageOptHelper.showHuanXinAvatar(Uri.parse(listItems.getUser().getUpic() + StringUtils.getQulity()), iv_item_album_member_avatar, mContext);
                    String notename = listItems.getUser().getNoteName();
                    String Unickname = listItems.getUser().getUnickname();
                    if (!StringUtils.isEmpty(notename)) {
                        tv_item_album_member_username.setText(notename);
                    } else {
                        tv_item_album_member_username.setText(Unickname);
                    }
                    // 点击列表
                    ll_item_album_member.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, UserStatusActivity.class);
                            intent.putExtra(CommonConstants.YNISCHAT, true);
                            intent.putExtra(CommonConstants.YNUSERID, listItems.getUser().getUserid());
                            intent.putExtra(CommonConstants.YNUSERNOTENAME, listItems.getUser().getNoteName());
                            intent.putExtra(CommonConstants.YNGROUPID, mGroup.getGroupid());
                            mContext.startActivity(intent);
                        }
                    });
                }
            }
        }
    }

    private YNMember getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.easechat_item_album_member, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        YNMember listItems = getItem(position);
        holder.bindData(listItems, position);
        holder.itemView.setTag(listItems);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }
}
